package com.treeroot.orderservice.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.treeroot.commonutils.AOP.Limit;
import com.treeroot.commonutils.result.R;
import com.treeroot.orderservice.order.entity.PcOrder;
import com.treeroot.orderservice.order.entity.PcPayLog;
import com.treeroot.orderservice.order.service.PcPayLogService;
import com.treeroot.servicebase.config.ApiVersion;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author TreeRoot
 * @since 2022-05-18
 */

//TODO:线程安全锁
@RestController
@Api(description = "支付")
@RequestMapping("/order/paylog")
@CrossOrigin
@ApiVersion(1.0)
public class PcPayLogController {

    @Autowired
    private PcPayLogService payLogService;


    /**
     * 支付 ....直接支付成功....然后插入到支付日志表
     * 订单号 支付完成时间  支付金额（分） 交易流水号  交易状态
     *
     * @param order 0未支付1已支付
     */
    @PostMapping("toPay")
    @Limit(key = "updatePlaneByIdOrder", permitsPerSecond = 1, timeout = 100, msg = "当前排队人数较多，请稍后再试！")
    public R payment(@RequestBody PcOrder order) {
        R r = payLogService.paymentOrRefund(order);
        return r;
    }

    /**
     * 支付日志表分页
     * @param current
     * @param limit
     * @return
     */
    @PostMapping("/LogPage/{current}/{limit}")
    public R getLogPage(@PathVariable long current, @PathVariable long limit
    ) {

        Map map = payLogService.getOrderList(current,limit);
        return R.ok().data(map);
    }


    /**
     * 支付日志表条件分页查询
     * @param current
     * @param limit
     * @param log
     * @return
     */
    @PostMapping("/payLog/{current}/{limit}")
    public R getLog(@PathVariable long current, @PathVariable long limit,
                    @RequestBody(required = false) PcPayLog log) {
        Map map = payLogService.getQueryLogList(current, limit, log);
        return R.ok().data(map);
    }

    /**
     * echarts  已完成支付统计
     */
    @GetMapping("count")
    public R count() {
        QueryWrapper<PcPayLog> wrapper = new QueryWrapper<>();
        wrapper.eq("trade_state", "已支付");
        return R.ok().data("logCount", payLogService.count(wrapper));
    }

}





